package cn.learn.athena.util;

import cn.learn.athena.exception.PageNotFoundException;
import cn.learn.athena.exception.ValidateException;
import org.apache.commons.lang.StringUtils;

/**
 * @author zlj
 * @Date 2018/11/7
 * @des
 */
public abstract class Validation {

    private Validation() {};

    public static void isPageNotFound(boolean expression) {
        if (expression) {
            throw new PageNotFoundException("找不到页面");
        }
    }

    public static void isTrue(boolean expression, String message) {
        if (!expression) {
            throwValidException(message);
        }

    }

    public static void isFalse(boolean expression, String message) {
        if (expression) {
            throwValidException(message);
        }

    }

    public static void isNull(Object object, String message ) {
        if (object != null) {
            throwValidException(message);
        }

    }

    public static void notNull(Object object, String message) {
        if (object == null) {
            throwValidException(message);
        }

    }


    public static void hasText(String text, String message ) {
        if (!StringUtils.isNotEmpty(text)) {
            throwValidException(message);
        }

    }



    public static void noNullElements(Object[] array, String message ) {
        if (array != null) {
            Object[] var3 = array;
            int var4 = array.length;

            for(int var5 = 0; var5 < var4; ++var5) {
                Object element = var3[var5];
                if (element == null) {
                    throwValidException(message);
                }
            }
        }

    }


    public static void isInstanceOf(Class<?> type, Object obj, String message) {
        if (!type.isInstance(obj)) {
            throwValidException(message);
        }

    }

    public static void throwValidException(String message) {
        throw new ValidateException(message);
    }
}
